/*
 * DataReaderDriver.java
 *
 * Created on December 28, 2007, 6:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.calculations;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.media.jai.registry.RIFRegistry;
import org.jfree.ui.RefineryUtilities;



/**
 *
 * @author joseacuna-rohter
 */
public class Volatility {
    
    private List<String[]> data;
    private List logDist;
    
    /** Creates a new instance of DataReaderDriver */
    public Volatility(List<String[]> data) {
        
        
        
        this.data = data;
        
       
        
        
        
        //Collections.reverse(data);
        
        logDist = logNormal(data);
        
        System.out.println("MEAN " + findMean(logDist));
        System.out.println("STDDEV " + stdDev(logDist,findMean(logDist)));
     
        System.out.println("Annualized Volatility " + 16*stdDev(logDist,findMean(logDist)));
        
        
        
        
    }
    

    
    public static List logNormal(List<String[]> data){
        
        double value = 0;
        double value2 = 0;
        List<String> roi = new ArrayList<String>();
        
        for(int i=0; i<data.size();i++){
            
            if(i>0){
                
                value = Double.parseDouble(data.get(i)[2]) / 
                        Double.parseDouble(data.get(i-1)[2]);
                
                value2 = Double.parseDouble(data.get(i)[2]) - 
                        Double.parseDouble(data.get(i-1)[2]);
                
                System.out.println("DIFF " + value2);
                
            }else{
                
            }
           
            
            if(value > 0){
                roi.add(""+ Math.log(value));
               
            }else{
                roi.add(""+ Double.MIN_VALUE);
            }
                
        }
        
        return roi;
    }
    
    public static double stdDev(List<String> data, double mean){
        
        double value = 0;
        
        for(int i=0; i<data.size();i++){
            value = value + Math.pow(Double.parseDouble(data.get(i)) - mean,2);
        }
        
        value = value/(data.size()-2);
        
        return Math.sqrt(value);
    }
    
    public static double findMean(List<String> data){
        
        double value = 0;
        
        for(int i=0; i<data.size();i++){
            value = value + Double.parseDouble(data.get(i));
            
        }
        
        return value/(data.size()-1);
    }
    
    public double getVolatility(){
        
        return 16*stdDev(logDist,findMean(logDist));
        
    }
    
}
